package net.awpspace.mobile.management.note;

import javax.microedition.midlet.MIDlet;

import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.spinner.Spinner;

import net.awpspace.mobile.form.BaseForm;

public class EditNote extends BaseForm {

	private Note note;
	private int index;
	private Label label_content, label_place, label_date, lable_alarm;
	private TextField name, content, place;
	private RadioButton alarm, no_alarm;
	private CheckBox t2, t3, t4, t5, t6, t7, cn;
	private Spinner day, month, year, hour, minite;
	private Container wapper, data_container, alarm_container,
			lable_hour_container, hour_container, repeat_alarm_container,
			button_container;

	public String BUTTON_SAVE = "Save";
	public String BUTTON_RESET = "Reset";
	
	private AudioManager audio = AudioManager.getInstance();

	public EditNote(MIDlet app, BaseForm parent, Note note, int index) {
		super(app, parent);
		this.note = note;
		this.index = index;
		setTransitionOutAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		// TODO Auto-generated constructor stub
		createUI();
	}

	private void createUI() {
		// TODO Auto-generated method stub
		wapper = new Container(new BoxLayout(BoxLayout.Y_AXIS));

		wapper.addComponent(new Label("Tên ghi chú:\n"));
		name = new TextField(note.getName());
		name.setRows(1);
		wapper.addComponent(name);

		label_content = new Label("Nội dung ghi chú:\n");
		wapper.addComponent(label_content);

		content = new TextField(note.getContent());
		content.setRows(5);
		wapper.addComponent(content);

		label_place = new Label("Địa điểm:\n");
		wapper.addComponent(label_place);

		place = new TextField(note.getPlace());
		place.setRows(3);
		wapper.addComponent(place);

		label_date = new Label("Ngày:\n");
		wapper.addComponent(label_date);

		data_container = new Container(new GridLayout(1, 3));
		day = Spinner.create(1, 32, note.getNgay(), 1);
		month = Spinner.create(1, 13, note.getThang(), 1);
		year = Spinner.create(1940, 2014, note.getNam(), 1);
		data_container.addComponent(day);
		data_container.addComponent(month);
		data_container.addComponent(year);
		wapper.addComponent(data_container);

		lable_alarm = new Label("Âm báo:\n");
		wapper.addComponent(lable_alarm);

		alarm_container = new Container(new GridLayout(2, 1));
		ButtonGroup groups = new ButtonGroup();
		alarm = new RadioButton();
		alarm.setText("Có");
		no_alarm = new RadioButton();
		no_alarm.setText("Không");
		groups.add(alarm);
		groups.add(no_alarm);
		alarm.setSelected(true);
		alarm.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				hour_container.setEnabled(true);
				repeat_alarm_container.setEnabled(true);
			}
		});
		no_alarm.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				hour_container.setEnabled(false);
				repeat_alarm_container.setEnabled(false);
			}
		});

		alarm_container.addComponent(alarm);
		alarm_container.addComponent(no_alarm);
		wapper.addComponent(alarm_container);

		lable_hour_container = new Container(new GridLayout(1, 2));
		lable_hour_container.addComponent(new Label("Giờ"));
		lable_hour_container.addComponent(new Label("Phút"));
		wapper.addComponent(lable_hour_container);

		hour_container = new Container(new GridLayout(1, 2));
		hour = Spinner.create(0, 23, 12, 1);
		minite = Spinner.create(0, 59, 30, 1);
		hour_container.addComponent(hour);
		hour_container.addComponent(minite);
		wapper.addComponent(hour_container);

		wapper.addComponent(new Label("Lặp âm báo:\n"));
		repeat_alarm_container = new Container(new GridLayout(7, 1));
		t2 = new CheckBox("Thứ 2");
		t2.setSelected(note.isT2());
		t3 = new CheckBox("Thứ 3");
		t3.setSelected(note.isT3());
		t4 = new CheckBox("Thứ 4");
		t4.setSelected(note.isT4());
		t5 = new CheckBox("Thứ 5");
		t5.setSelected(note.isT5());
		t6 = new CheckBox("Thứ 6");
		t6.setSelected(note.isT6());
		t7 = new CheckBox("Thứ 7");
		t7.setSelected(note.isT7());
		cn = new CheckBox("Chủ nhật");
		cn.setSelected(note.isCn());
		repeat_alarm_container.addComponent(cn);
		repeat_alarm_container.addComponent(t2);
		repeat_alarm_container.addComponent(t3);
		repeat_alarm_container.addComponent(t4);
		repeat_alarm_container.addComponent(t5);
		repeat_alarm_container.addComponent(t6);
		repeat_alarm_container.addComponent(t7);
		wapper.addComponent(repeat_alarm_container);

		button_container = new Container(new GridLayout(1, 2));
		button_container.addComponent(new Button(new Command(BUTTON_SAVE)));
		button_container.addComponent(new Button(new Command(BUTTON_RESET)));
		wapper.addComponent(button_container);

		this.addComponent(wapper);
	}

	protected void processCommand(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getCommand().getCommandName().equals(BUTTON_RESET)) {
			audio.playSample("/button.mp3");
			name.setText("");
			content.setText("");
			place.setText("");
			no_alarm.setSelected(true);
		} else if (e.getCommand().getCommandName().equals(BUTTON_SAVE)) {
			audio.playSample("/button.mp3");
			Note note;
			String ten = name.getText();
			String cont = content.getText();
			String pla = place.getText();
			int ngay = Integer.parseInt(String.valueOf(day.getSelectedItem()));
			int thang = Integer
					.parseInt(String.valueOf(month.getSelectedItem()));
			int nam = Integer.parseInt(String.valueOf(year.getSelectedItem()));
			boolean on_alarm;
			if (alarm.isSelected()) {
				on_alarm = true;
				int gio = Integer.parseInt(String.valueOf(hour
						.getSelectedItem()));
				int phut = Integer.parseInt(String.valueOf(minite
						.getSelectedItem()));
				boolean chunhat = cn.isSelected();
				boolean thu2 = t2.isSelected();
				boolean thu3 = t3.isSelected();
				boolean thu4 = t4.isSelected();
				boolean thu5 = t5.isSelected();
				boolean thu6 = t6.isSelected();
				boolean thu7 = t7.isSelected();

				note = new Note(ten, cont, pla, ngay, thang, nam, on_alarm,
						gio, phut, chunhat, thu2, thu3, thu4, thu5, thu6, thu7);
			} else {
				on_alarm = false;
				note = new Note(ten, cont, pla, ngay, thang, nam, on_alarm);
			}
			Dao.editNote(note, index);
			new ViewNotes(app, NoteManagement.getInstance()).show();
		}
	}

}
